package com.fypg.module.medical.service.doctorapplication;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fypg.framework.common.util.object.BeanUtils;
import com.fypg.framework.common.util.servlet.ServletUtils;
import com.fypg.module.medical.controller.app.doctorapplication.vo.AppDoctorApplicationReqVO;
import com.fypg.module.medical.controller.app.doctorapplication.vo.AppDoctorApplicationRespVO;
import com.fypg.module.medical.dal.dataobject.doctorapplication.DoctorApplicationDO;
import com.fypg.module.medical.dal.mysql.doctorapplication.DoctorApplicationMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 医生应用列表 Service 实现类
 *
 * @author PJY
 */
@Service
@Validated
public class DoctorApplicationServiceImpl implements DoctorApplicationService {

    @Resource
    private DoctorApplicationMapper doctorApplicationMapper;


    @Override
    public List<AppDoctorApplicationRespVO> getDoctorApplicationList(AppDoctorApplicationReqVO reqVO) {
        List<DoctorApplicationDO> doctorApplicationDOList = doctorApplicationMapper.selectListByHealthCode(reqVO);
        List<AppDoctorApplicationRespVO> doctorApplicationRespVOS = BeanUtils.toBean(doctorApplicationDOList, AppDoctorApplicationRespVO.class);

        if (!doctorApplicationRespVOS.isEmpty()) {
            String baseURL = ServletUtils.buildBaseUrlFromCurrentRequest();

            doctorApplicationRespVOS.forEach(vo -> {
                if (StringUtils.isNotBlank(vo.getIconUrl()) && !vo.getIconUrl().startsWith("http")) {
                    // 如果 iconPath 以 "/" 开头，则去掉。（测试数据库中的数据是 / 开头）
                    String iconPath = vo.getIconUrl();
                    if (iconPath.startsWith("/")) {
                        iconPath = iconPath.substring(1);
                    }
                    vo.setIconUrl(baseURL + iconPath);
                }
            });
        }

        return doctorApplicationRespVOS;
    }
}
